{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c952b50e",
   "metadata": {
    "papermill": {
     "duration": 0.029358,
     "end_time": "2022-04-14T02:32:17.513699",
     "exception": false,
     "start_time": "2022-04-14T02:32:17.484341",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Before you get started\n",
    "In this notebook we are about to finish the credit card approval prediction task with pandas and [Towhee](https://towhee.io/). Towhee is an open-source machine learning pipeline that helps you with various machine learning tasks. Make sure you have installed towhee before you get started via `pip install towhee`.\n",
    "\n",
    "You are more than welcome to join our community, together we can make a differnece.\n",
    "\n",
    "**Github**: [https://github.com/towhee-io/towhee](https://github.com/towhee-io/towhee)\n",
    "\n",
    "**Slack**: [https://slack.towhee.io](https://slack.towhee.io)\n",
    "\n",
    "**Twitter**: [https://twitter.com/towheeio](https://twitter.com/towheeio)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "1cbb7a07",
   "metadata": {
    "papermill": {
     "duration": 0.036677,
     "end_time": "2022-04-14T02:32:32.378853",
     "exception": false,
     "start_time": "2022-04-14T02:32:32.342176",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Data Processing With Pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0e024ef9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6d9dac59",
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install towhee imblearn pandas"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d199529d",
   "metadata": {
    "papermill": {
     "duration": 0.036071,
     "end_time": "2022-04-14T02:32:32.451324",
     "exception": false,
     "start_time": "2022-04-14T02:32:32.415253",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "First load the data from [Kaggle](https://www.kaggle.com/code/chizzzy/credit-card-approval-prediction/data?scriptVersionId=92959791) as Dataframe for further processing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ec52769e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0\n",
      "100 3109k  100 3109k    0     0  1228k      0 --:--:--  0:00:02 --:--:--     0 0  0:00:02  0:00:02 --:--:-- 7492k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\n",
      "100 2338k  100 2338k    0     0  1401k      0  0:00:01  0:00:01 --:--:-- 8433k\n"
     ]
    }
   ],
   "source": [
    "! curl -L https://github.com/towhee-io/examples/releases/download/data/application_record.csv.zip -O\n",
    "! curl -L https://github.com/towhee-io/examples/releases/download/data/credit_record.csv.zip -O\n",
    "! unzip -q -o application_record.csv.zip\n",
    "! unzip -q -o credit_record.csv.zip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ffd00114",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:32.530021Z",
     "iopub.status.busy": "2022-04-14T02:32:32.529503Z",
     "iopub.status.idle": "2022-04-14T02:32:34.330276Z",
     "shell.execute_reply": "2022-04-14T02:32:34.329717Z",
     "shell.execute_reply.started": "2022-04-11T03:40:19.109076Z"
    },
    "papermill": {
     "duration": 1.842774,
     "end_time": "2022-04-14T02:32:34.330389",
     "exception": false,
     "start_time": "2022-04-14T02:32:32.487615",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "record = pd.read_csv(\"./credit_record.csv\", encoding = 'utf-8')\n",
    "data = pd.read_csv(\"./application_record.csv\", encoding = 'utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffb36a91",
   "metadata": {
    "papermill": {
     "duration": 0.035288,
     "end_time": "2022-04-14T02:32:34.402223",
     "exception": false,
     "start_time": "2022-04-14T02:32:34.366935",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Find the first month that users' data were recorded and rename the column with a more understandable name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9992d7a3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:34.487367Z",
     "iopub.status.busy": "2022-04-14T02:32:34.486708Z",
     "iopub.status.idle": "2022-04-14T02:32:34.528286Z",
     "shell.execute_reply": "2022-04-14T02:32:34.528666Z",
     "shell.execute_reply.started": "2022-04-11T03:40:20.988663Z"
    },
    "papermill": {
     "duration": 0.089854,
     "end_time": "2022-04-14T02:32:34.528851",
     "exception": false,
     "start_time": "2022-04-14T02:32:34.438997",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "begin_month=pd.DataFrame(record.groupby([\"ID\"])[\"MONTHS_BALANCE\"].agg(min))\n",
    "begin_month=begin_month.rename(columns={'MONTHS_BALANCE':'begin_month'}) "
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "364f66eb",
   "metadata": {
    "papermill": {
     "duration": 0.0349,
     "end_time": "2022-04-14T02:32:34.599460",
     "exception": false,
     "start_time": "2022-04-14T02:32:34.564560",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Process the `STATUS` column to find out if candidates have the record of overdue. Here is a table describe what each label stands for:\n",
    "- X: No loan for the month, labeled as -1;\n",
    "- C: paid off that month, labeled as -1;\n",
    "- 0: 1-29 days past due;\n",
    "- 1: 30-59 days past due;\n",
    "- 2: 60-89 days overdue;\n",
    "- 3: 90-119 days overdue;\n",
    "- 4: 120-149 days overdue;\n",
    "- 5: Overdue or bad debts, write-offs for more than 150 days "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bacb8f9d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:34.824355Z",
     "iopub.status.busy": "2022-04-14T02:32:34.823739Z",
     "iopub.status.idle": "2022-04-14T02:32:40.628346Z",
     "shell.execute_reply": "2022-04-14T02:32:40.627844Z",
     "shell.execute_reply.started": "2022-04-11T03:40:21.032615Z"
    },
    "papermill": {
     "duration": 5.993645,
     "end_time": "2022-04-14T02:32:40.628491",
     "exception": false,
     "start_time": "2022-04-14T02:32:34.634846",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       " 0    0.754202\n",
       "-1    0.129455\n",
       " 1    0.101838\n",
       " 2    0.007307\n",
       " 5    0.004241\n",
       " 3    0.001914\n",
       " 4    0.001044\n",
       "Name: STATUS, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "record.loc[record['STATUS']=='X', 'STATUS']=-1 \n",
    "record.loc[record['STATUS']=='C', 'STATUS']=-1 \n",
    "record.loc[record['STATUS']=='0', 'STATUS']=0 \n",
    "record.loc[record['STATUS']=='1', 'STATUS']=1\n",
    "record.loc[record['STATUS']=='2', 'STATUS']=2\n",
    "record.loc[record['STATUS']=='3', 'STATUS']=3 \n",
    "record.loc[record['STATUS']=='4', 'STATUS']=4 \n",
    "record.loc[record['STATUS']=='5', 'STATUS']=5\n",
    "record.groupby('ID')['STATUS'].max().value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4393927e",
   "metadata": {
    "papermill": {
     "duration": 0.055881,
     "end_time": "2022-04-14T02:32:40.740024",
     "exception": false,
     "start_time": "2022-04-14T02:32:40.684143",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Generally, users in risk should be less than 3%, thus those who overdue for more than 60 days should be marked as risk users."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "8be3b34c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:40.975272Z",
     "iopub.status.busy": "2022-04-14T02:32:40.974625Z",
     "iopub.status.idle": "2022-04-14T02:32:41.174523Z",
     "shell.execute_reply": "2022-04-14T02:32:41.175056Z",
     "shell.execute_reply.started": "2022-04-11T03:40:26.558472Z"
    },
    "papermill": {
     "duration": 0.378935,
     "end_time": "2022-04-14T02:32:41.175218",
     "exception": false,
     "start_time": "2022-04-14T02:32:40.796283",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dep_value\n",
       "No           0.985495\n",
       "Yes          0.014505\n",
       "dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "record.loc[record['STATUS']>=2, 'dep_value']=1\n",
    "record.loc[record['STATUS']<2, 'dep_value']=0 \n",
    "temp = record[['ID', 'dep_value']].groupby('ID').sum()\n",
    "temp.loc[temp['dep_value']!=0, 'dep_value']='Yes'\n",
    "temp.loc[temp['dep_value']==0, 'dep_value']= 'No'\n",
    "temp.value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12cf0054",
   "metadata": {
    "papermill": {
     "duration": 0.055425,
     "end_time": "2022-04-14T02:32:41.287065",
     "exception": false,
     "start_time": "2022-04-14T02:32:41.231640",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Merge the information into one dataframe, and mark those risk users with target `1` while other users `0`. We will regard the `target` column as result. Meanwhile, we should drop those rows with missing values to avoid disturb."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e5b94714",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:41.402226Z",
     "iopub.status.busy": "2022-04-14T02:32:41.401627Z",
     "iopub.status.idle": "2022-04-14T02:32:41.815494Z",
     "shell.execute_reply": "2022-04-14T02:32:41.814933Z",
     "shell.execute_reply.started": "2022-04-11T03:40:26.88881Z"
    },
    "papermill": {
     "duration": 0.472325,
     "end_time": "2022-04-14T02:32:41.815639",
     "exception": false,
     "start_time": "2022-04-14T02:32:41.343314",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "new_data=pd.merge(data,begin_month,how=\"left\",on=\"ID\")\n",
    "new_data=pd.merge(new_data, temp,how='inner',on='ID')\n",
    "new_data['target']=new_data['dep_value']\n",
    "new_data.loc[new_data['target']=='Yes','target']=1\n",
    "new_data.loc[new_data['target']=='No','target']=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "221121d0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:41.935283Z",
     "iopub.status.busy": "2022-04-14T02:32:41.934583Z",
     "iopub.status.idle": "2022-04-14T02:32:41.958007Z",
     "shell.execute_reply": "2022-04-14T02:32:41.957601Z",
     "shell.execute_reply.started": "2022-04-11T03:40:27.26873Z"
    },
    "papermill": {
     "duration": 0.085904,
     "end_time": "2022-04-14T02:32:41.958115",
     "exception": false,
     "start_time": "2022-04-14T02:32:41.872211",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>CODE_GENDER</th>\n",
       "      <th>FLAG_OWN_CAR</th>\n",
       "      <th>FLAG_OWN_REALTY</th>\n",
       "      <th>CNT_CHILDREN</th>\n",
       "      <th>AMT_INCOME_TOTAL</th>\n",
       "      <th>NAME_INCOME_TYPE</th>\n",
       "      <th>NAME_EDUCATION_TYPE</th>\n",
       "      <th>NAME_FAMILY_STATUS</th>\n",
       "      <th>NAME_HOUSING_TYPE</th>\n",
       "      <th>DAYS_BIRTH</th>\n",
       "      <th>DAYS_EMPLOYED</th>\n",
       "      <th>FLAG_MOBIL</th>\n",
       "      <th>FLAG_WORK_PHONE</th>\n",
       "      <th>FLAG_PHONE</th>\n",
       "      <th>FLAG_EMAIL</th>\n",
       "      <th>OCCUPATION_TYPE</th>\n",
       "      <th>CNT_FAM_MEMBERS</th>\n",
       "      <th>begin_month</th>\n",
       "      <th>dep_value</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5008804</td>\n",
       "      <td>M</td>\n",
       "      <td>Y</td>\n",
       "      <td>Y</td>\n",
       "      <td>0</td>\n",
       "      <td>427500.0</td>\n",
       "      <td>Working</td>\n",
       "      <td>Higher education</td>\n",
       "      <td>Civil marriage</td>\n",
       "      <td>Rented apartment</td>\n",
       "      <td>-12005</td>\n",
       "      <td>-4542</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-15.0</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5008805</td>\n",
       "      <td>M</td>\n",
       "      <td>Y</td>\n",
       "      <td>Y</td>\n",
       "      <td>0</td>\n",
       "      <td>427500.0</td>\n",
       "      <td>Working</td>\n",
       "      <td>Higher education</td>\n",
       "      <td>Civil marriage</td>\n",
       "      <td>Rented apartment</td>\n",
       "      <td>-12005</td>\n",
       "      <td>-4542</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-14.0</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5008806</td>\n",
       "      <td>M</td>\n",
       "      <td>Y</td>\n",
       "      <td>Y</td>\n",
       "      <td>0</td>\n",
       "      <td>112500.0</td>\n",
       "      <td>Working</td>\n",
       "      <td>Secondary / secondary special</td>\n",
       "      <td>Married</td>\n",
       "      <td>House / apartment</td>\n",
       "      <td>-21474</td>\n",
       "      <td>-1134</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Security staff</td>\n",
       "      <td>2.0</td>\n",
       "      <td>-29.0</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5008808</td>\n",
       "      <td>F</td>\n",
       "      <td>N</td>\n",
       "      <td>Y</td>\n",
       "      <td>0</td>\n",
       "      <td>270000.0</td>\n",
       "      <td>Commercial associate</td>\n",
       "      <td>Secondary / secondary special</td>\n",
       "      <td>Single / not married</td>\n",
       "      <td>House / apartment</td>\n",
       "      <td>-19110</td>\n",
       "      <td>-3051</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Sales staff</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-4.0</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5008809</td>\n",
       "      <td>F</td>\n",
       "      <td>N</td>\n",
       "      <td>Y</td>\n",
       "      <td>0</td>\n",
       "      <td>270000.0</td>\n",
       "      <td>Commercial associate</td>\n",
       "      <td>Secondary / secondary special</td>\n",
       "      <td>Single / not married</td>\n",
       "      <td>House / apartment</td>\n",
       "      <td>-19110</td>\n",
       "      <td>-3051</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Sales staff</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-26.0</td>\n",
       "      <td>No</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        ID CODE_GENDER FLAG_OWN_CAR FLAG_OWN_REALTY  CNT_CHILDREN  \\\n",
       "0  5008804           M            Y               Y             0   \n",
       "1  5008805           M            Y               Y             0   \n",
       "2  5008806           M            Y               Y             0   \n",
       "3  5008808           F            N               Y             0   \n",
       "4  5008809           F            N               Y             0   \n",
       "\n",
       "   AMT_INCOME_TOTAL      NAME_INCOME_TYPE            NAME_EDUCATION_TYPE  \\\n",
       "0          427500.0               Working               Higher education   \n",
       "1          427500.0               Working               Higher education   \n",
       "2          112500.0               Working  Secondary / secondary special   \n",
       "3          270000.0  Commercial associate  Secondary / secondary special   \n",
       "4          270000.0  Commercial associate  Secondary / secondary special   \n",
       "\n",
       "     NAME_FAMILY_STATUS  NAME_HOUSING_TYPE  DAYS_BIRTH  DAYS_EMPLOYED  \\\n",
       "0        Civil marriage   Rented apartment      -12005          -4542   \n",
       "1        Civil marriage   Rented apartment      -12005          -4542   \n",
       "2               Married  House / apartment      -21474          -1134   \n",
       "3  Single / not married  House / apartment      -19110          -3051   \n",
       "4  Single / not married  House / apartment      -19110          -3051   \n",
       "\n",
       "   FLAG_MOBIL  FLAG_WORK_PHONE  FLAG_PHONE  FLAG_EMAIL OCCUPATION_TYPE  \\\n",
       "0           1                1           0           0             NaN   \n",
       "1           1                1           0           0             NaN   \n",
       "2           1                0           0           0  Security staff   \n",
       "3           1                0           1           1     Sales staff   \n",
       "4           1                0           1           1     Sales staff   \n",
       "\n",
       "   CNT_FAM_MEMBERS  begin_month dep_value target  \n",
       "0              2.0        -15.0        No      0  \n",
       "1              2.0        -14.0        No      0  \n",
       "2              2.0        -29.0        No      0  \n",
       "3              1.0         -4.0        No      0  \n",
       "4              1.0        -26.0        No      0  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.set_option('display.max_columns', None)\n",
    "pd.set_option('display.max_rows', 100)\n",
    "new_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a96587c1",
   "metadata": {
    "papermill": {
     "duration": 0.037306,
     "end_time": "2022-04-14T02:32:42.032661",
     "exception": false,
     "start_time": "2022-04-14T02:32:41.995355",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Before we get started, we should take a rough look at our samples, in case inbalanced data leading to a weird result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dedf8ae8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:42.108892Z",
     "iopub.status.busy": "2022-04-14T02:32:42.108421Z",
     "iopub.status.idle": "2022-04-14T02:32:42.166927Z",
     "shell.execute_reply": "2022-04-14T02:32:42.166524Z",
     "shell.execute_reply.started": "2022-04-11T03:40:27.296954Z"
    },
    "papermill": {
     "duration": 0.097338,
     "end_time": "2022-04-14T02:32:42.167032",
     "exception": false,
     "start_time": "2022-04-14T02:32:42.069694",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    24712\n",
       "1      422\n",
       "Name: target, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_data = new_data.dropna()\n",
    "new_data['target'].value_counts()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "5c9ff7bb",
   "metadata": {
    "papermill": {
     "duration": 0.037275,
     "end_time": "2022-04-14T02:32:42.241837",
     "exception": false,
     "start_time": "2022-04-14T02:32:42.204562",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Obviously the data are extremely imbalance, so we'll need to resample the data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0d74f89a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:32:42.318065Z",
     "iopub.status.busy": "2022-04-14T02:32:42.317568Z",
     "iopub.status.idle": "2022-04-14T02:32:57.881822Z",
     "shell.execute_reply": "2022-04-14T02:32:57.882384Z",
     "shell.execute_reply.started": "2022-04-11T03:40:27.358877Z"
    },
    "papermill": {
     "duration": 15.603823,
     "end_time": "2022-04-14T02:32:57.882558",
     "exception": false,
     "start_time": "2022-04-14T02:32:42.278735",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from imblearn.over_sampling import SMOTEN\n",
    "X = new_data[['ID', 'CODE_GENDER', 'FLAG_OWN_CAR', 'FLAG_OWN_REALTY', 'CNT_CHILDREN', 'AMT_INCOME_TOTAL', 'NAME_INCOME_TYPE', \n",
    "       'NAME_EDUCATION_TYPE', 'NAME_FAMILY_STATUS', 'NAME_HOUSING_TYPE', 'DAYS_BIRTH', 'DAYS_EMPLOYED', 'FLAG_MOBIL',\n",
    "       'FLAG_WORK_PHONE', 'FLAG_PHONE', 'FLAG_EMAIL', 'OCCUPATION_TYPE', 'CNT_FAM_MEMBERS', 'begin_month','dep_value']]\n",
    "y = new_data['target'].astype('int')\n",
    "X_balance,y_balance = SMOTEN().fit_resample(X, y)\n",
    "X_balance = pd.DataFrame(X_balance, columns = X.columns)\n",
    "X_balance.insert(0, 'target', y_balance)\n",
    "new_data = X_balance\n",
    "new_data['years_birth'] = new_data.DAYS_BIRTH.map(lambda x: -int(x)//365)\n",
    "new_data['years_employed'] = new_data.DAYS_EMPLOYED.map(lambda x: -int(x)//365)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9fd236a1",
   "metadata": {
    "papermill": {
     "duration": 0.056877,
     "end_time": "2022-04-14T02:32:57.997379",
     "exception": false,
     "start_time": "2022-04-14T02:32:57.940502",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Building Models with [Towhee](https://towhee.io/) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f109bdd2",
   "metadata": {
    "papermill": {
     "duration": 0.057367,
     "end_time": "2022-04-14T02:32:58.112046",
     "exception": false,
     "start_time": "2022-04-14T02:32:58.054679",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "In the following part, we will use Towhee's DataCollection API to deal with the processed data. DataCollection provides a series API to support training, prediction, and evaluating with machine learning models.\n",
    "\n",
    "Also, Towhee has encapsulated several models as built-in operators, which we will introduce in the following training part."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f4dc821d",
   "metadata": {
    "papermill": {
     "duration": 0.057355,
     "end_time": "2022-04-14T02:32:58.227213",
     "exception": false,
     "start_time": "2022-04-14T02:32:58.169858",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Prepare Training And Testing Data"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "424be47d",
   "metadata": {
    "papermill": {
     "duration": 0.059879,
     "end_time": "2022-04-14T02:32:58.346926",
     "exception": false,
     "start_time": "2022-04-14T02:32:58.287047",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "We need to split the original dataset into training and testing dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5acfac7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train_data, test_data = train_test_split(new_data, test_size=0.2)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "3274f291",
   "metadata": {
    "papermill": {
     "duration": 0.057608,
     "end_time": "2022-04-14T02:33:07.105869",
     "exception": false,
     "start_time": "2022-04-14T02:33:07.048261",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Feature Extract"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0e261f9b",
   "metadata": {
    "papermill": {
     "duration": 0.057457,
     "end_time": "2022-04-14T02:33:07.221168",
     "exception": false,
     "start_time": "2022-04-14T02:33:07.163711",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Then we need to process some data, include:\n",
    "- Discretize the numerical data, both continuous data and binary data;\n",
    "- Encode categorical data with non-digital value;\n",
    "- Stack chosen features into a feature tensor;\n",
    "\n",
    "First step is to create models and fit with the correspoding columns."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c2f8fbbf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-14T02:33:07.338934Z",
     "iopub.status.busy": "2022-04-14T02:33:07.338272Z",
     "iopub.status.idle": "2022-04-14T02:33:07.346504Z",
     "shell.execute_reply": "2022-04-14T02:33:07.347070Z",
     "shell.execute_reply.started": "2022-04-11T03:40:53.38578Z"
    },
    "papermill": {
     "duration": 0.068638,
     "end_time": "2022-04-14T02:33:07.347244",
     "exception": false,
     "start_time": "2022-04-14T02:33:07.278606",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import KBinsDiscretizer, OneHotEncoder\n",
    "from scipy import sparse\n",
    "import numpy as np\n",
    "\n",
    "discretizers = {}\n",
    "encoders = {}\n",
    "\n",
    "for col in [\n",
    "    'CNT_CHILDREN',\n",
    "    'AMT_INCOME_TOTAL',\n",
    "    'years_birth',\n",
    "    'years_employed',\n",
    "    'CNT_FAM_MEMBERS'\n",
    "    ]:\n",
    "    if col == 'CNT_CHILDREN':\n",
    "        discretizers[col] = KBinsDiscretizer(n_bins=3, encode='onehot', strategy='quantile')\n",
    "    else:\n",
    "        discretizers[col] = KBinsDiscretizer(n_bins=5, encode='onehot', strategy='quantile')\n",
    "    discretizers[col].fit(train_data[col].values.reshape(-1, 1))\n",
    "\n",
    "for col in [\n",
    "    'NAME_INCOME_TYPE',\n",
    "    'OCCUPATION_TYPE',\n",
    "    'NAME_HOUSING_TYPE',\n",
    "    'NAME_EDUCATION_TYPE',\n",
    "    'NAME_FAMILY_STATUS',\n",
    "    'CODE_GENDER',\n",
    "    'FLAG_OWN_CAR',\n",
    "    'FLAG_OWN_REALTY'\n",
    "    ]:\n",
    "    encoders[col] = OneHotEncoder()\n",
    "    encoders[col].fit(train_data[col].values.reshape(-1, 1))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "81d8dbcc",
   "metadata": {},
   "source": [
    "Then we can define functions to transform the date with the fitted models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "322a1baf",
   "metadata": {},
   "outputs": [],
   "source": [
    "class NumDiscretizer:\n",
    "    def __init__(self, col_name):\n",
    "        self._model = discretizers[col_name]\n",
    "\n",
    "    def __call__(self, data):\n",
    "        data = np.array([data]).reshape([-1, 1])\n",
    "        return self._model.transform(data)\n",
    "\n",
    "class CateOneHotEncoder:\n",
    "    def __init__(self, col_name):\n",
    "        self._model = encoders[col_name]\n",
    "\n",
    "    def __call__(self, data):\n",
    "        data = np.array([data]).reshape([-1, 1])\n",
    "        return self._model.transform(data)\n",
    "\n",
    "def tensor_hstack(*arg):\n",
    "    return sparse.hstack(arg)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6bb56604",
   "metadata": {},
   "source": [
    "Load data from pandas dataframe, discretize the numerical data, encode categorical data, and stack these features into a feature tensor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85978e54",
   "metadata": {},
   "outputs": [],
   "source": [
    "from towhee import pipe, ops\n",
    "\n",
    "feature_extract = (\n",
    "\tpipe.input('df')\n",
    "        .flat_map('df', tuple(new_data), lambda x: [i for _, i in x.iterrows()])\n",
    "        .map('NAME_INCOME_TYPE', 'inctp', CateOneHotEncoder(col_name='NAME_INCOME_TYPE'))\n",
    "        .map('OCCUPATION_TYPE', 'occyp', CateOneHotEncoder(col_name='OCCUPATION_TYPE'))\n",
    "        .map('NAME_HOUSING_TYPE', 'houtp', CateOneHotEncoder(col_name='NAME_HOUSING_TYPE'))\n",
    "        .map('NAME_EDUCATION_TYPE', 'edutp', CateOneHotEncoder(col_name='NAME_EDUCATION_TYPE'))\n",
    "        .map('NAME_FAMILY_STATUS', 'famtp', CateOneHotEncoder(col_name='NAME_FAMILY_STATUS'))\n",
    "        .map('CODE_GENDER', 'gender', CateOneHotEncoder(col_name='CODE_GENDER'))\n",
    "        .map('FLAG_OWN_CAR', 'car', CateOneHotEncoder(col_name='FLAG_OWN_CAR'))\n",
    "        .map('FLAG_OWN_REALTY', 'realty', CateOneHotEncoder(col_name='FLAG_OWN_REALTY'))\n",
    "        .map('CNT_CHILDREN', 'childnum', NumDiscretizer(col_name='CNT_CHILDREN'))\n",
    "        .map('AMT_INCOME_TOTAL', 'inc', NumDiscretizer(col_name='AMT_INCOME_TOTAL'))\n",
    "        .map('years_birth', 'age', NumDiscretizer(col_name='years_birth'))\n",
    "        .map('years_employed', 'worktm', NumDiscretizer(col_name='years_employed'))\n",
    "        .map('CNT_FAM_MEMBERS', 'fmsize', NumDiscretizer(col_name='CNT_FAM_MEMBERS'))\n",
    "        .map(\n",
    "            ('childnum', 'inc', 'age', 'worktm', 'fmsize', 'inctp', 'occyp', 'houtp', 'edutp', 'famtp','gender', 'car', 'realty'),\n",
    "            'fea',\n",
    "            tensor_hstack\n",
    "        )\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e208c033",
   "metadata": {
    "papermill": {
     "duration": 0.056981,
     "end_time": "2022-04-14T02:33:07.461401",
     "exception": false,
     "start_time": "2022-04-14T02:33:07.404420",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### Model"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "622cafdc",
   "metadata": {
    "papermill": {
     "duration": 0.056407,
     "end_time": "2022-04-14T02:33:07.575011",
     "exception": false,
     "start_time": "2022-04-14T02:33:07.518604",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "In this tutorial, we will user logistic regression, decision tree, and support vector machine."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f4df6700",
   "metadata": {
    "papermill": {
     "duration": 0.05615,
     "end_time": "2022-04-14T02:33:07.688093",
     "exception": false,
     "start_time": "2022-04-14T02:33:07.631943",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "#### Train\n",
    "\n",
    "- Load the feature tensors and labels\n",
    "- feed the features and lables to logistic regression model, decision tree model and support vector machine model for training."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "85c8b6f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from towhee.utils.sklearn_utils import LogisticRegression, DecisionTreeClassifier, svm\n",
    "from towhee.utils.scipy_utils import sparse\n",
    "LR = LogisticRegression(max_iter=10)\n",
    "DT = DecisionTreeClassifier(splitter='random', max_depth=10)\n",
    "SVM = svm.SVC(C = 0.8, kernel='rbf', probability=True)\n",
    "\n",
    "def lr_fit(fea, tar):\n",
    "    X = sparse.vstack(fea)\n",
    "    y = np.array(tar).reshape([-1, 1])\n",
    "    LR.fit(X, y)\n",
    "\n",
    "def dt_fit(fea, tar):\n",
    "    X = sparse.vstack(fea)\n",
    "    y = np.array(tar).reshape([-1, 1])\n",
    "    DT.fit(X, y)\n",
    "\n",
    "def svm_fit(fea, tar):\n",
    "    X = sparse.vstack(fea)\n",
    "    y = np.array(tar).reshape([-1, 1])\n",
    "    SVM.fit(X, y)\n",
    "\n",
    "def lr_predict(fea):\n",
    "    return LR.predict(fea)[0]\n",
    "\n",
    "def dt_predict(fea):\n",
    "    return DT.predict(fea)[0]\n",
    "\n",
    "def svm_predict(fea):\n",
    "    return SVM.predict(fea)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "849629cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "train = (\n",
    "\tfeature_extract.output('fea', 'target')\n",
    ")\n",
    "train_size = len(train_data)\n",
    "res = train(train_data[:train_size])\n",
    "x = [res.get() for _ in range(train_size)]\n",
    "fea = [i[0] for i in x]\n",
    "tar = [i[1] for i in x]\n",
    "\n",
    "lr_fit(fea, tar)\n",
    "dt_fit(fea, tar)\n",
    "svm_fit(fea, tar)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "146317a6",
   "metadata": {
    "papermill": {
     "duration": 0.037362,
     "end_time": "2022-04-14T02:45:33.029879",
     "exception": false,
     "start_time": "2022-04-14T02:45:32.992517",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "#### Evaluate"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bbb540d1",
   "metadata": {
    "papermill": {
     "duration": 0.03739,
     "end_time": "2022-04-14T02:46:10.606931",
     "exception": false,
     "start_time": "2022-04-14T02:46:10.569541",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The trained models are ready and the states are properly stored, we can predict with them and see how they works.\n",
    "\n",
    "- Run the same feature extract procedure on testing set to get the feature tensor\n",
    "- Predict the result with three models we trained previously\n",
    "- Compare the predicted result and actual result, calculate accuracy and recall"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b1bfc46f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score, recall_score\n",
    "\n",
    "def cal_accuracy(predicted_list, actual_list):\n",
    "\treturn accuracy_score(actual_list, predicted_list)\n",
    "\n",
    "def cal_recall(predicted_list, actual_list):\n",
    "\treturn recall_score(actual_list, predicted_list, average='weighted')\n",
    "\n",
    "def get_confusion_matrix(predicted_list, actual_list):\n",
    "\treturn confusion_matrix(actual_list, predicted_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bf88ba57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table style=\"border-collapse: collapse;\"><tr><th style=\"text-align: center; font-size: 130%; border: none;\">ID</th> <th style=\"text-align: center; font-size: 130%; border: none;\">target</th> <th style=\"text-align: center; font-size: 130%; border: none;\">lr_pre</th> <th style=\"text-align: center; font-size: 130%; border: none;\">dt_pre</th> <th style=\"text-align: center; font-size: 130%; border: none;\">svm_pre</th> <th style=\"text-align: center; font-size: 130%; border: none;\">lr_accuracy</th> <th style=\"text-align: center; font-size: 130%; border: none;\">lr_recall</th> <th style=\"text-align: center; font-size: 130%; border: none;\">dt_accuracy</th> <th style=\"text-align: center; font-size: 130%; border: none;\">dt_recall</th> <th style=\"text-align: center; font-size: 130%; border: none;\">svm_accuracy</th> <th style=\"text-align: center; font-size: 130%; border: none;\">svm_recall</th></tr> <tr><td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">5047662</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td></tr> <tr><td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">5091321</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td></tr> <tr><td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">5009628</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td></tr> <tr><td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">5051160</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td></tr> <tr><td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">5022044</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">1</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.7542741527567021</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.8801213960546282</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td> <td style=\"text-align: center; vertical-align: center; border-right: solid 1px #D3D3D3; border-left: solid 1px #D3D3D3; \">0.9757207890743551</td></tr></table>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "eval =(\n",
    "\tfeature_extract.map('fea', 'lr_pre', lr_predict)\n",
    "\t\t.map('fea', 'dt_pre', dt_predict)\n",
    "\t\t.map('fea', 'svm_pre', svm_predict)\n",
    "\t\t.window_all(('lr_pre', 'target'), 'lr_accuracy', cal_accuracy)\n",
    "\t\t.window_all(('lr_pre', 'target'), 'lr_recall', cal_recall)\n",
    "\t\t.window_all(('dt_pre', 'target'), 'dt_accuracy', cal_accuracy)\n",
    "\t\t.window_all(('dt_pre', 'target'), 'dt_recall', cal_recall)\n",
    "\t\t.window_all(('svm_pre', 'target'), 'svm_accuracy', cal_accuracy)\n",
    "\t\t.window_all(('svm_pre', 'target'), 'svm_recall', cal_recall)\n",
    "\t\t.output(\n",
    "\t\t\t'ID', 'target', 'lr_pre', 'dt_pre', 'svm_pre',\n",
    "\t\t\t'lr_accuracy', 'lr_recall', 'dt_accuracy', 'dt_recall', 'svm_accuracy', 'svm_recall',\n",
    "\t\t\ttracer=True\n",
    "\t\t)\n",
    ")\n",
    "\n",
    "res = eval(test_data)\n",
    "from towhee.datacollection import DataCollection\n",
    "res = DataCollection(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1ae6ca65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.remove('credit_record.csv.zip')\n",
    "os.remove('application_record.csv.zip')\n",
    "os.remove('credit_record.csv')\n",
    "os.remove('application_record.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 1203.361231,
   "end_time": "2022-04-14T02:52:09.531633",
   "environment_variables": {},
   "exception": null,
   "input_path": "__notebook__.ipynb",
   "output_path": "__notebook__.ipynb",
   "parameters": {},
   "start_time": "2022-04-14T02:32:06.170402",
   "version": "2.3.3"
  },
  "vscode": {
   "interpreter": {
    "hash": "f7dd10cdbe9a9c71f7e71741efd428241b5f9fa0fecdd29ae07a5706cd5ff8a2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
